package com.xlcszsgc.module.clob.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xlcszsgc.core.page.CalculatePage;
import com.xlcszsgc.core.page.PageInfoBean;
import com.xlcszsgc.core.util.PropertyUtil;
import com.xlcszsgc.module.Constants;
import com.xlcszsgc.module.clob.bao.IClobDao;
import com.xlcszsgc.module.clob.domain.BaseClob;
import com.xlcszsgc.module.clob.service.IClobService;
import com.xlcszsgc.module.clob.ui.commond.BaseClobCommond;

/**
 * @Title: ClobServiceImpl.java
 * @Package com.xlcszsgc.module.clob.service.impl
 * @Description: clob服务层实现
 * @author Zhaoqt  
 * @date 2014-5-26 上午1:20:01
 * @version V1.0
 */
@Service(value = "com.xlcszsgc.module.clob.service.impl.ClobServiceImpl")
public class ClobServiceImpl implements IClobService {

	@Resource(name = "com.xlcszsgc.module.clob.bao.impl.ClobDaoImpl")
	private IClobDao clobDao;
	
	@Override
	public void addClob(BaseClob baseClob) throws Exception {
		baseClob.setDateTime(new Date());
		baseClob.setPublish(Constants.IS_PUBLISH);
		baseClob.setSortNum(0);
		clobDao.addClob(baseClob);
	}

	@Override
	public void deleteClobById(BaseClob baseClob) throws Exception {
		clobDao.deleteClobById(baseClob);
	}

	@Override
	public void updateClob(BaseClob baseClob) throws Exception {
		if(!PropertyUtil.objectNotEmpty(baseClob.getDateTime())) {
			baseClob.setDateTime(new Date());
		}
		baseClob.setPublish(Constants.NO_PUBLISH);
		clobDao.updateClob(baseClob);
	}

	@Override
	public List<BaseClob> listClob(BaseClobCommond baseClobCommond) throws Exception {
		if(baseClobCommond != null) {
			String hql = "select c.clobId as clobId, c.title as title, c.categoryCode as categoryCode, c.publish as publish, c.sortNum as sortNum, c.dateTime as dateTime " +
					"from BaseClob c where 0=0 ";
			Map<String, Object> paramMap = baseClobCommond.getParamMap();
			if(PropertyUtil.objectNotEmpty(baseClobCommond.getSearchClobId())) {
				hql += " and c.clobId = :searchClobId";
				paramMap.put("searchClobId", baseClobCommond.getSearchClobId());
			}
			if(PropertyUtil.objectNotEmpty(baseClobCommond.getSearchCategoryCode())) {
				hql += " and c.categoryCode = :searchCategoryCode";
				paramMap.put("searchCategoryCode", baseClobCommond.getSearchCategoryCode());
			}
			if(PropertyUtil.objectNotEmpty(baseClobCommond.getSearchTitle())) {
				hql += " and c.title like %':searchTitle%'";
				paramMap.put("searchTitle", baseClobCommond.getSearchTitle());
			}
			if(PropertyUtil.objectNotEmpty(baseClobCommond.getSearchPublish())) {
				hql += " and c.publish = :searchPublish";
				paramMap.put("searchPublish", baseClobCommond.getSearchPublish());
			}
			hql += " order by c.sortNum asc ";
			baseClobCommond.setQueryString(hql);
			if(baseClobCommond.isPaging()) {
				PageInfoBean pageInfoBean = baseClobCommond.getPageBean();
				Long pageCount = clobDao.countBlob(baseClobCommond);
				if(pageCount != null && pageCount.intValue() > 0) {
					CalculatePage.calculate(pageInfoBean, pageCount.intValue());
					return clobDao.listClob(baseClobCommond, pageInfoBean.getFirstResult()
							, pageInfoBean.getMaxResult());
				}
			} else {
				return clobDao.listClob(baseClobCommond, -1, -1);
			}
			
		}
		return null;
	}

	@Override
	public BaseClob getBaseClob(BaseClobCommond baseClobCommond)
			throws Exception {
		if(!PropertyUtil.objectNotEmpty(baseClobCommond.getSearchClobId())) {
			baseClobCommond.setSearchClobId(baseClobCommond.getSearchClobIds()[0]);
		}
		if(PropertyUtil.objectNotEmpty(baseClobCommond.getSearchClobId())) {
			return clobDao.findObject(baseClobCommond);
		}
		return null;
	}

	@Override
	public void batchdeleteClobById(BaseClobCommond baseClobCommond) throws Exception {
		String[] searchClobIds = baseClobCommond.getSearchClobIds();
		if(PropertyUtil.objectNotEmpty(searchClobIds)) {
			for(String searchClobId : searchClobIds) {
				BaseClob baseClob = new BaseClob();
				baseClob.setClobId(searchClobId);
				clobDao.deleteClobById(baseClob);
			}
		}
	}

	@Override
	public void batchDynamicUpdateClob(BaseClobCommond baseClobCommond)
			throws Exception {
		String[] searchClobIds = baseClobCommond.getSearchClobIds();
		BaseClob baseClob =  baseClobCommond.getBaseClob();
		if(PropertyUtil.objectNotEmpty(searchClobIds) 
				&& PropertyUtil.objectNotEmpty(baseClob)) {
			for(String searchClobId : searchClobIds) {
				Map<String, Object> paramMap = baseClobCommond.getParamMap();
				String hql = "update BaseClob c set ";
				if(PropertyUtil.objectNotEmpty(baseClob.getPublish())) {
					hql += "c.publish = :publish, ";
					paramMap.put("publish", baseClob.getPublish());
				}
				if(PropertyUtil.objectNotEmpty(baseClob.getSortNum())) {
					hql += "c.sortNum = :sortNum, ";
					paramMap.put("sortNum", baseClob.getSortNum());		
				}
				hql += "c.clobId= :clobId ";
				paramMap.put("clobId", searchClobId);
				hql +=  "where c.clobId = :searchClobId";
				paramMap.put("searchClobId", searchClobId);	
				baseClobCommond.setQueryString(hql);
				clobDao.dynamicUpdateClob(baseClobCommond);
			}
		}
	}
	
}
